package com.example.springblog.controller;

import com.example.springblog.constant.Constants;
import com.example.springblog.model.BlogInfo;
import com.example.springblog.model.Result;
import com.example.springblog.service.BlogService;
import com.example.springblog.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @GetMapping("/getList")
    public Result<BlogInfo> getList() {
        log.info("获取图书列表");
        List<BlogInfo> blogInfos = blogService.getBlogList();
        if(blogInfos != null) {
            return Result.success(blogInfos);
        }else {
            log.error("获取图书列表失败!");
            return Result.fail("内部错误,请联系管理员!");
        }
    }

    /**
     * 根据博客id获取博客详情
     * @param blogId
     * @return
     */
    @GetMapping("/getBlogDetail")
    public Result<BlogInfo> getBlogDetail(Integer blogId, HttpServletRequest request) {
        log.info("根据博客id:{} 获取博客", blogId);
        // 参数校验
        if(blogId == null || blogId <= 0) {
            return Result.fail("博客id有误!");
        }
        // 获取博客
        BlogInfo blogInfo = blogService.getBlogDetail(blogId);
        // 获取当前登录用户id
        String jwtToken = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        Integer loginUserId = JwtUtils.getUerIdFromToken(jwtToken);
        if(loginUserId != null) {
            blogInfo.setIsLoginUser(loginUserId == blogInfo.getUserId());
        }
        if(blogInfo != null) {
            return Result.success(blogInfo);
        }else {
            log.error("根据博客id:{}获取博客失败!", blogId);
            return Result.fail("内部错误,请联系管理员!");
        }
    }

    /**
     * 发布博客
     * @param title
     * @param content
     * @param request
     * @return
     */
    @PostMapping("/add")
    public Result<String> addBlog(String title, String content, HttpServletRequest request) {
        // 获取当前登录用户id
        String jwtToken = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        Integer userId = JwtUtils.getUerIdFromToken(jwtToken);

        if(userId == null || userId < 0) {
            return Result.fail("用户登录异常!");
        }

        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setTitle(title);
        blogInfo.setUserId(userId);
        blogInfo.setContent(content);
        int result = blogService.addBlog(blogInfo);
        if(result > 0) {
            return Result.success("");
        }
        return Result.fail("发布博客失败!");
    }

    /**
     * 更新博客
     * @param blogInfo
     * @return
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody BlogInfo blogInfo) {
        log.info("博客更新, 获取参数blogInfo:{}", blogInfo);
        // 参数校验
        if(blogInfo == null || blogInfo.getId() == null) {
            return Result.fail("参数有误!");
        }
        // 更新
        int result = blogService.updateBlog(blogInfo);
        if(result > 0) {
            return Result.success("");
        }
        return Result.fail("文章更新失败!");
    }

    /**
     * 博客删除
     * @param blogId
     * @return
     */
    @PostMapping("/delete")
    public Result<String> delete(Integer blogId) {
        log.info("博客删除, 获取参数blogId:{}", blogId);
        // 参数校验
        if(blogId == null || blogId <= 0) {
            return Result.fail("博客id有误!");
        }
        // 删除
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        int result = blogService.updateBlog(blogInfo);
        if(result > 0) {
            return Result.success("");
        }
        return Result.fail("文章删除失败!");
    }
}
